﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;

namespace Ewts.BaseData
{
    public partial class AddAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dropCompany.DataSource = new DataLib.BLL.Cumtomer().GetAllList();
                dropCompany.DataTextField = "companyname";
                dropCompany.DataValueField = "id";
                dropCompany.DataBind();
            }
            
         
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr=CheckInput();
            if (strErr == "")
            {
                User newuser = new User();
                newuser.UserName = txtName.Text;
                newuser.TrueName = txtTruename.Text;
                newuser.Password =  AccountsPrincipal.EncryptPassword(txtPwd.Text);
                newuser.Phone = this.txtphone.Text.Trim();
                newuser.Email = txtemail.Text;
                newuser.Department = "客户";
                newuser.UserType = dropCategory.SelectedValue;
                int useid = newuser.Create();
                if (useid != -100)
                {
                    DataLib.Model.cum_aacounts cumacc = new DataLib.Model.cum_aacounts();
                    cumacc.cumid = int.Parse(dropCompany.SelectedValue);
                    cumacc.accountid = useid;
                    if (new DataLib.BLL.cum_aacounts().Add(cumacc))
                    {
                        DataLib.Common.MessageBox.Show(this, "新增成功！");
                        ClearText();
                    }
                }
            }
            else
            {
                DataLib.Common.MessageBox.Show(this, strErr);
                return;
            }
        }

        protected string CheckInput()
        {
            string strErr = "";
             if (txtName.Text == "" || txtName.Text == null) strErr += "账户名不能为空！\r\n";
            if (txtPwd.Text == "" || txtPwd.Text == null) strErr += "密码不能为空！\r\n";
            if (txtTruename.Text == ""||txtTruename.Text == null) strErr += "真实姓名不能为空！\r\n";
             
            
            if (new User().HasUser(txtName.Text))
            {
                strErr += "该用户名已存在！";
            }
            return strErr;
        }

        protected void ClearText()
        {
            txtName.Text = "";
            txtPwd.Text = "";
            txtTruename.Text = "";
            txtphone.Text = "";
            txtemail.Text = "";
         }
    }
}